package cn.haut.online_read.controller.sanwenzx;

import cn.haut.online_read.crawler.sanwenzx.ArticleCrawler;
import cn.haut.online_read.crawler.sanwenzx.ArticleCrawler2;
import cn.haut.online_read.crawler.sanwenzx.ArticleCrawler3;
import cn.haut.online_read.utils.CrawlerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author xing
 * @date 2021-05-22 14:54
 */
@Controller("aricleController")
@RequestMapping("/sanwenzx")
@Slf4j
public class ArticleController {

    public static final String BASE_URL = "http://sanwenzx.com";

//    @GetMapping(value = {"/{category}/{major}/{year}/{url}", "/{major}/{url}"})
//    @ResponseBody
//    public String get (
//            @PathVariable(value = "category", required = false) String category,
//            @PathVariable("major") String major,
//            @PathVariable(value = "year", required = false) String year,
//            @PathVariable("url") String url
//    ) {
//        // 包含数字，代表要看的页面不是文章页面，可能是专栏页面
//        if(major.matches("[0-9]*")) {
//            // TODO
//            log.error("地址包含数字！");
//            return null;
//        }
//
//        String crawlerUrl = "";
//        if(category == null) {
//            crawlerUrl = String.format("%s/%s/%s", BASE_URL, major, url);
//        } else {
//            crawlerUrl = String.format("%s/%s/%s/%s/%s", BASE_URL, category, major, year, url);
//        }
//        String jsonResult = CrawlerUtils.getJsonResult(new ArticleCrawler(), crawlerUrl);
//        log.error("爬取成功");
//        return jsonResult;
//    }


    @GetMapping(value = {"/getArticle"})
    @ResponseBody
    public String get (
            @RequestParam("url") String url
    ) {
        return CrawlerUtils.getJsonResult(new ArticleCrawler3(), BASE_URL + url);
    }
}
